package com.example.demo.generator.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.example.demo.generator.entiy.ResultMsg;
import com.example.demo.generator.entiy.TRole;
import com.example.demo.generator.entiy.TUser;
import com.example.demo.generator.entiy.TUserRole;
import com.example.demo.generator.service.TUserRoleService;
import com.example.demo.generator.service.TUserService;
import com.example.demo.generator.util.JwtUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("user")
@CrossOrigin("*")
@Api(tags = "用户接口")
public class UserController {
    @Autowired
    public JwtUtil JwtUtil;
    @Autowired
    public TUserService tUserService;
    @Autowired
    public TUserRoleService tUserRoleService;

    @ApiOperation("获取用户列表")
    @GetMapping("getuserlist")
    //获取用户列表
    public R getuserlist(@RequestParam(value = "pageNum",defaultValue = "")Integer pageNum,@RequestParam(value = "pageSize",defaultValue = "")Integer pageSize,@RequestParam(value = "name",defaultValue = "")String name){
        if(pageNum!=null&&pageSize!=null) {
            if(name!=null) {
                QueryWrapper<TUser>tUserQueryWrapper=new QueryWrapper<>();
                tUserQueryWrapper.like("name",name);
                PageHelper.startPage(pageNum, pageSize);
                List<TUser> tUserList = tUserService.list(tUserQueryWrapper);
                PageInfo<TUser>pageInfo=new PageInfo<>(tUserList);
                return R.ok(pageInfo);
            }else{
                PageHelper.startPage(pageNum,pageSize);
                List<TUser>tUserList=tUserService.list(null);
                PageInfo<TUser>pageInfo=new PageInfo<>(tUserList);
                return R.ok(pageInfo);
            }
        }else{
            List<TUser>tUserList=tUserService.list(null);
            return R.ok(tUserList);
        }
    }

    @ApiOperation("用户验证")
    @GetMapping("login")
    //登录
    public R login(@RequestParam("username")String username,@RequestParam("password")String password){
        QueryWrapper<TUser>userQueryWrapper=new QueryWrapper<>();
        userQueryWrapper.eq("name",username);
        userQueryWrapper.eq("password",password);
        TUser tUser=tUserService.getOne(userQueryWrapper);
        //生成JWT字符串
        String token = JwtUtil.createToken(tUser.getId().toString(),tUser.getName());
        ResultMsg msg=new ResultMsg();
        msg.setName(tUser.getName());
        msg.setToken(token);
        return R.ok(msg);
    }

    @GetMapping("getuserrole")
    @ApiOperation("获取用户角色")
    //用户角色
    public R getuserrole(@RequestParam("uid")Integer uid){
        List<TRole>roleList=tUserRoleService.getuserrole(uid);
        return R.ok(roleList);
    }

    @PostMapping("edituserrole")
    @ApiOperation("编辑用户角色")
    public R edituserrole(@RequestParam("uid")Integer uid,@RequestParam("rids")String rids){
        try {
            ArrayList<String> rolearray = new ArrayList<>();
            if (rids.contains(",")) {
                rolearray = new ArrayList<>(Arrays.asList(rids.split(",")));
            } else {
                rolearray.add(rids);
            }
            //先清除
            QueryWrapper<TUserRole>tUserRoleQueryWrapper=new QueryWrapper<>();
            tUserRoleQueryWrapper.eq("uid",uid);
            tUserRoleService.remove(tUserRoleQueryWrapper);
            //再添加
            for(int i=0;i<rolearray.size();i++){
                TUserRole tUserRole=new TUserRole();
                tUserRole.setRid(Integer.parseInt(rolearray.get(i)));
                tUserRole.setUid(uid);
                tUserRoleService.save(tUserRole);
            }
            return R.ok("success");
        }
        catch (Exception ex){
            return R.failed(ex.toString());
        }
    }
}
